﻿using System;
using System.Configuration;
using ZilLion.Core.Log;

using ZilLion.TaskManager.Repository;

namespace ZilLion.Core.TaskManager.Client.SystemMonitor
{
    /// <summary>
    ///     节点心跳监控者
    ///     用于心跳通知数据库当前节点状态
    /// </summary>
    public class NodeHeartBeatMonitor : BaseMonitor
    {
        private readonly TaskNodeInfoRespository _taskNodeInfoRespository = new TaskNodeInfoRespository();

        public override int Interval
        {
            get { return 5000; }
        }

        protected override void Run()
        {
            try
            {
                if (_taskNodeInfoRespository.NodeHeartbeat(ConfigurationManager.AppSettings["NodeId"]))
                {
                    ZilLionLogManager.GetLogger().Trace($"{ConfigurationManager.AppSettings["NodeId"]}节点发送心跳成功");
                }
                else
                {
                    ZilLionLogManager.GetLogger().Warn($"{ConfigurationManager.AppSettings["NodeId"]}节点发送心跳失败");
                }
             
            }
            catch (Exception ex)
            {
                ZilLionLogManager.GetLogger().Error(ex, tags: $"{ConfigurationManager.AppSettings["NodeId"]}节点发送心跳失败");
            }
        }
    }
}